/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include <QMessageBox>
#include <QSettings>
#include <QShowEvent>

#include <query.h>
#include "cereparteneri.h"
#include "parteneri.h"

using namespace SQL;

CereParteneri::CereParteneri(QWidget* parent, Qt::WFlags fl)
		: KontAMabilWindow(parent, fl), Ui::cereParteneriForm()
{
	setupUi(this);
}

CereParteneri::~CereParteneri()
{
}

void CereParteneri::seteazaTextele()
{
	denumireEdit->setText(defaultDatabase()->query("parteneri")->value("denumire").toString());
	cifEdit->setText(defaultDatabase()->query("parteneri")->value("cif").toString());
}

void CereParteneri::showEvent(QShowEvent * event)
{
	KontAMabilWindow::showEvent(event);
	if (!event->isAccepted())
		return;
	QSettings s;
	if (s.value("parteneriPlugin/maiIntaiDenumirea",true).toBool())
	{
		denumireEdit->setFocus();
		cereDenumireButton->setChecked(true);
	}
	else
	{
		cifEdit->setFocus();
		cereCifButton->setChecked(true);
	}

}

void CereParteneri::accept()
{
	QSettings s;
	s.setValue("parteneriPlugin/maiIntaiDenumirea",cereDenumireButton->isChecked());
	KontAMabilWindow::accept();
}

void CereParteneri::on_denumireEdit_returnPressed()
{
	if (!denumireEdit->text().length())
		cifEdit->setFocus();
	else
		if (defaultDatabase()->query("parteneri")->value("denumire")==denumireEdit->text())
			accept();
}

void CereParteneri::on_cifEdit_returnPressed()
{
	if (!cifEdit->text().length())
		denumireEdit->setFocus();
	else
		if (!defaultDatabase()->query("parteneri")->locate("cif",cifEdit->text(),false,false,false))
		{
			if (QMessageBox::question(this,tr("Partener negasit"),tr("Partenerul cu C.I.F.-ul %1 nu a fost gasit.\nDoriti sa-l adaugati?").arg(cifEdit->text()),QMessageBox::Yes|QMessageBox::No)==QMessageBox::Yes)
			{
				bool res;
				Parteneri * widget = new Parteneri;//(parent);
				res=widget->addPartener("", 0, 0, cifEdit->text());
				delete widget;
				if (res)
				{
					accept();
					return;
				}
			}
			cifEdit->selectAll();
		}
		else
			accept();
}
